package com.example.wallpaperapp.ui.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wallpaperapp.R;
import com.example.wallpaperapp.adapter.TitleFragmentPagerAdapter;
import com.example.wallpaperapp.ui.fragment.MonthFragment;
import com.example.wallpaperapp.ui.fragment.NowuserFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class NowuseActivity extends AppCompatActivity implements  ViewPager.OnPageChangeListener , View.OnClickListener{
    private ViewPager mViewpager;
    List<Fragment> fragments;
    private TabLayout mTabLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nowuse);

        mViewpager = findViewById(R.id.nowuse_viewpager);
        mTabLayout = findViewById(R.id.nowuse_tab2);

        ImageView imageButton=findViewById(R.id.nowuse_return);
        imageButton.setOnClickListener(this);
        initViewPager();

    }

    private void initViewPager() {
        fragments = new ArrayList<>();
        fragments.add(new NowuserFragment());
        fragments.add(new MonthFragment());



        mViewpager.setAdapter(new TitleFragmentPagerAdapter(getSupportFragmentManager(),  fragments,new String[]{"当前使用", "历史"}));
        mViewpager.addOnPageChangeListener(this);
        mTabLayout.setupWithViewPager(mViewpager);

        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_item_texts);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(NowuseActivity.this, R.style.TabLayoutTextSize);
            }
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.tab_item_texts);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextAppearance(NowuseActivity.this, R.style.TabLayoutTextSize_two);
            }
            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        mViewpager.setCurrentItem(0);

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }




    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.nowuse_return:
                finish();
                break;
            default:
                break;
        }
    }
}